def pluggable(*functions):
    for f in functions:
        f.pluggable = True

_outgoing = {}
def BUILD(shell, name, operation, data = ''):
    if operation == 'APPEND':
        if name in _outgoing:
            _outgoing[name].append(data)
        else:
            _outgoing[name] = [data]

def SENDMSG(shell, name, subject, *recipients):
    msg = _outgoing.get(name)
    if msg is not None:
        del _outgoing[name]
        msg = '\n'.join(msg)
        msg = msg.encode('zlib').encode('base64')
        for to in recipients:
            shell.sendMessage('!inter-plug [%s] RECVMSG %s %s' % \
                              (to, subject, msg))

_incoming = {}
def RECVMSG(shell, subject, data):
    msg = data.decode('base64').decode('zlib').split('\n')
    _incoming[subject] = msg

    return 'Received %r' % subject

pluggable(BUILD, SENDMSG, RECVMSG)
